package com.mes.handler.process;

import com.mes.data.EmesProcess;
import com.mes.data.EmesProcessRoute;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.application.process.BaseProcessActionHandler;
import org.openbravo.dal.service.OBDal;
import java.util.Map;

public class EmesProcessRouteActionHandler extends BaseProcessActionHandler{

	private static final Logger logger = Logger.getLogger(EmesProcessRouteActionHandler.class);
	
	@Override
	protected JSONObject doExecute(Map<String, Object> parameters, String content) {
		JSONObject responseResult = new JSONObject();
		JSONObject message = new JSONObject();
		try {
			responseResult.put("message", message);
			JSONObject request = new JSONObject(content);
			JSONArray selection = request.getJSONObject("_params").getJSONObject("emes_process_route_id").getJSONArray("_selection");
			if(selection.length() == 0 ){//请选择记录
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "请选择一条目标工艺路线");
				return responseResult;
			}else{
				String fromId = request.getString("inpemesProcessRouteId");
				String toId = selection.getJSONObject(0).getString("id");
				return this.copyRoute(fromId, toId);
			}
		} catch (JSONException e) {
			OBDal.getInstance().rollbackAndClose();
			try {
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "复制失败");
			} catch (JSONException e1) {
				logger.error(e1);
			}
			logger.error(e);
			
		}
		return responseResult;
	}
	
	private JSONObject copyRoute(String fromId,String toId){
		JSONObject responseResult = new JSONObject();
		JSONObject message = new JSONObject();
		try{
			responseResult.put("message", message);
			EmesProcessRoute toRoute = OBDal.getInstance().get(EmesProcessRoute.class,toId);
			if(toRoute == null ){
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "目标工艺路线不存在");
				return responseResult;
			}
			if(toRoute.getEmesProcessList().size() > 0 ){
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "复制失败，目标工艺路线的工序已经存在 ");
				return responseResult;
			}
			EmesProcessRoute processRoute = OBDal.getInstance().get(EmesProcessRoute.class,fromId);
			if(processRoute == null ){
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "工艺路线不存在");
				return responseResult;
			}
			
			EmesProcess copyRoute = null;
			//复制工序
			for (EmesProcess route : processRoute.getEmesProcessList()) {
				copyRoute = new EmesProcess();
				copyRoute.setEmesProcessRoute(toRoute);
				copyRoute.setRouteNo(route.getRouteNo());
				copyRoute.setName(route.getName());
				copyRoute.setEmesProcessOpr(route.getEmesProcessOpr());
				copyRoute.setPrepareTime(route.getPrepareTime());
				copyRoute.setProcessTime(route.getProcessTime());
				copyRoute.setTransportTime(route.getTransportTime());
				copyRoute.setPrescriptionTime(route.getPrescriptionTime());
				copyRoute.setConvertRate(route.getConvertRate());
				copyRoute.setNextRouteNo(route.getNextRouteNo());
				copyRoute.setWageCategory(route.getWageCategory());
				copyRoute.setScrapRate(route.getScrapRate());
				copyRoute.setTransferBatch(route.getTransferBatch());
				copyRoute.setWageCalculationCategory(route.getWageCalculationCategory());
				OBDal.getInstance().save(copyRoute);
			}
			OBDal.getInstance().commitAndClose();
		}catch(Exception e){
			OBDal.getInstance().rollbackAndClose();
			try {
				responseResult.put("retryExecution",true);
				message.put("severity", "error");
				message.put("text", "复制失败 ");
			} catch (JSONException e1) {
				logger.error(e1);
			}
			logger.error(e);
		}
		return responseResult;
	}

}
